package com.example.service;

import com.example.entity.OrderSort;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.BatchingRabbitTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

// 订单处理服务
@Component
@Slf4j
public class OrderProcessor {
    
    @Autowired
    private RabbitTemplate rabbitTemplate;

//    @Autowired
//    private BatchingRabbitTemplate batchingRabbitTemplate;
    
    @Autowired
    private OrderPriorityService orderPriorityService;

    // 注入已配置的ObjectMapper
    @Autowired
    private ObjectMapper objectMapper;
    
    /**
     * 处理不同类型订单的示例
     */
    public void processVariousOrders() {
        List<OrderSort> orders = Arrays.asList(
            new OrderSort("ORD001", "普通", "普通", 299.0, LocalDateTime.now()),
            new OrderSort("ORD002", "VIP", "普通", 1500.0, LocalDateTime.now()),
            new OrderSort("ORD003", "普通", "紧急", 800.0, LocalDateTime.now()),
            new OrderSort("ORD004", "商家", "普通", 50000.0, LocalDateTime.now()),
            new OrderSort("ORD005", "普通", "普通", 99.0, LocalDateTime.now()),
            new OrderSort("ORD006", "VIP", "紧急", 3500.0, LocalDateTime.now()),
            new OrderSort("ORD007", "普通", "故障", 0.0, LocalDateTime.now()), // 系统故障订单
            new OrderSort("ORD008", "商家", "紧急", 20000.0, LocalDateTime.now())
        );
        
        // 发送所有订单到队列
        for (OrderSort order : orders) {
            sendOrderToQueue(order);
        }
    }
    
    private void sendOrderToQueue(OrderSort order) {
        int priority = orderPriorityService.determineOrderPriority(order);
        
        // 设置消息属性
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setPriority(priority);
        messageProperties.setContentType("application/json");
        
        try {
            //这里因为用了ObjectMapper，导致了java.time.LocalDateTimenot supported by default为了解决这个所以加下面三行
            String orderJson = objectMapper.writeValueAsString(order);
            Message message = new Message(orderJson.getBytes(), messageProperties);
            rabbitTemplate.send("order.priority.exchange", "order.priority", message);
            log.info("订单已发送至队列 - ID: {}, 客户类型: {}, 订单类型: {}, 金额: {}, 优先级: {}",
                    order.getOrderId(), order.getCustomerType(), order.getOrderType(),
                    order.getAmount(), priority);
        } catch (Exception e) {
            log.error("发送订单消息失败: {}", order.getOrderId(), e);
        }
    }


}